import java.io.*;
import java.util.*;

public class HighscoreManager {

		private ArrayList<String> names = null;
		private ArrayList<Float> scores = null;
		private String tableName = "default";
		
		public HighscoreManager ( String fileName ) {
			tableName = fileName;
		}
		
		public void loadScoreTable () {
			try {
				// Load the names
				FileInputStream nameFile = new FileInputStream(tableName+".nameTable");
				FileInputStream scoreFile = new FileInputStream(tableName+".scoreTable");
				ObjectInputStream nameIn = new ObjectInputStream(nameFile);
				ObjectInputStream scoreIn = new ObjectInputStream(scoreFile);

				names = (ArrayList<String>) nameIn.readObject();
				scores = (ArrayList<Float>) scoreIn.readObject();

				nameIn.close();
		        scoreIn.close();
		        nameFile.close();
		        scoreFile.close();
		        
			}catch(ClassNotFoundException | IOException e){
				// if we reach this point, we know that the file may not exist, or there was a problem reading the map.
				// if this is the case, simply create an empty score map.
				names = new ArrayList<String>();
				scores = new ArrayList<Float>();
				
				return;
			}
		}
		
		public void saveScoreTable () {
			try {
		         FileOutputStream nameFile = new FileOutputStream(tableName+".nameTable");
		         FileOutputStream scoreFile = new FileOutputStream(tableName+".scoreTable");
		         ObjectOutputStream nameOut = new ObjectOutputStream(nameFile);
		         ObjectOutputStream scoreOut = new ObjectOutputStream(scoreFile);

		         nameOut.writeObject( names );
		         scoreOut.writeObject( scores );
		         
		         nameOut.close();
		         scoreOut.close();
		         nameFile.close();
		         scoreFile.close();
		         
		      }catch(IOException i) {
		          i.printStackTrace();
		      }
		}
		
		public void swapScores ( int a, int b ) {
			String tmpS = names.get(a);
			names.set(a, names.get(b));
			names.set(b, tmpS);
			
			Float tmpF = scores.get(a);
			scores.set(a, scores.get(b));
			scores.set(b, tmpF);
		}
		
		public void sortScores () {
			// basically a selection sort...
			// iterate through all scores, one by one.
			for ( int i = 0; i < scores.size(); i ++ ) {
				// find the current minimum from the index to the end...
				float minval = Float.POSITIVE_INFINITY;
				int minind = i;
				for ( int j = i; j < scores.size(); j ++ ) {
					if ( scores.get(j) < minval ) {
						minval = scores.get(j);
						minind = j;
					}
				}
				// swap the minimum index with the current at I
				swapScores( i, minind );		
			}
			
			// and at this point, the array should be sorted!.
		}
		
		public void clearScores () {
			loadScoreTable();
			names.clear();
			scores.clear();
			saveScoreTable();
		}
		
		public int scoreCount ( ) {
			return scores.size();
		}
		
		public String nameForIndex ( int i) {
			return names.get(i);
		}
		public float scoreForIndex ( int i ) {
			return scores.get(i);
		}
		
		public void submitScore ( String name, float score ) {
			if ( names == null || scores == null )
				loadScoreTable();
			
			names.add(name);
			scores.add(score);
			
			sortScores();
			
			saveScoreTable();
		}
	
}
